#!/bin/bash

# 企业微信H5应用部署验证脚本
# 用于验证企业微信登录功能是否正常工作

echo "=== 企业微信H5应用部署验证 ==="
echo "开始时间: $(date)"
echo ""

# 配置信息
BASE_URL="http://localhost:6724"
FRONTEND_URL="http://localhost:5173"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查函数
check_service() {
    local name=$1
    local url=$2
    local expected_status=${3:-200}
    
    echo -n "检查 $name ... "
    
    if command -v curl >/dev/null 2>&1; then
        response=$(curl -s -o /dev/null -w "%{http_code}" "$url" 2>/dev/null)
        if [ "$response" = "$expected_status" ]; then
            echo -e "${GREEN}✓ 正常${NC} (HTTP $response)"
            return 0
        else
            echo -e "${RED}✗ 异常${NC} (HTTP $response)"
            return 1
        fi
    else
        echo -e "${YELLOW}⚠ 跳过${NC} (curl未安装)"
        return 2
    fi
}

# 检查端口
check_port() {
    local port=$1
    local service=$2
    
    echo -n "检查端口 $port ($service) ... "
    
    if command -v netstat >/dev/null 2>&1; then
        if netstat -tuln 2>/dev/null | grep -q ":$port "; then
            echo -e "${GREEN}✓ 监听中${NC}"
            return 0
        else
            echo -e "${RED}✗ 未监听${NC}"
            return 1
        fi
    elif command -v ss >/dev/null 2>&1; then
        if ss -tuln 2>/dev/null | grep -q ":$port "; then
            echo -e "${GREEN}✓ 监听中${NC}"
            return 0
        else
            echo -e "${RED}✗ 未监听${NC}"
            return 1
        fi
    else
        echo -e "${YELLOW}⚠ 跳过${NC} (netstat/ss未安装)"
        return 2
    fi
}

# 检查文件
check_file() {
    local file=$1
    local description=$2
    
    echo -n "检查 $description ... "
    
    if [ -f "$file" ]; then
        echo -e "${GREEN}✓ 存在${NC}"
        return 0
    else
        echo -e "${RED}✗ 不存在${NC}"
        return 1
    fi
}

# 检查目录
check_directory() {
    local dir=$1
    local description=$2
    
    echo -n "检查 $description ... "
    
    if [ -d "$dir" ]; then
        echo -e "${GREEN}✓ 存在${NC}"
        return 0
    else
        echo -e "${RED}✗ 不存在${NC}"
        return 1
    fi
}

echo "1. 基础服务检查"
echo "=================="

# 检查后端服务端口
check_port 6724 "后端API服务"

# 检查前端服务端口
check_port 5173 "前端开发服务"

echo ""
echo "2. HTTP服务检查"
echo "================"

# 检查后端API
check_service "后端API健康检查" "$BASE_URL/health" 200
check_service "Swagger文档" "$BASE_URL/swagger" 200

# 检查前端服务
check_service "前端应用" "$FRONTEND_URL" 200

echo ""
echo "3. 企业微信相关接口检查"
echo "======================"

# 检查企业微信登录接口（需要授权码，预期401或400）
check_service "企业微信登录接口" "$BASE_URL/api/auth/wechat-login" 401

# 检查企业微信JS-SDK配置接口（需要认证，预期401）
check_service "JS-SDK配置接口" "$BASE_URL/api/wechat/js-config" 401

echo ""
echo "4. 关键文件检查"
echo "================"

# 检查后端关键文件
check_file "./Controllers/AuthController.cs" "认证控制器"
check_file "./Controllers/WeChatController.cs" "企业微信控制器"
check_file "./Services/IWeChatService.cs" "企业微信服务接口"
check_file "./Services/WeChatService.cs" "企业微信服务实现"
check_file "./Middleware/WeChatAccessMiddleware.cs" "企业微信访问中间件"

# 检查前端关键文件
check_file "./RefundAuditAndroid/src/views/LoginMobile.vue" "移动端登录页面"
check_file "./RefundAuditAndroid/src/views/WeChatTest.vue" "企业微信测试页面"
check_file "./RefundAuditAndroid/src/stores/auth.js" "认证状态管理"
check_file "./RefundAuditAndroid/src/utils/wechat.js" "企业微信工具类"

echo ""
echo "5. 配置文件检查"
echo "================"

# 检查配置文件
check_file "./appsettings.json" "应用配置文件"
check_file "./appsettings.Development.json" "开发环境配置"

# 检查企业微信配置
if [ -f "./appsettings.json" ]; then
    echo -n "检查企业微信配置 ... "
    if grep -q "WeChat" "./appsettings.json" 2>/dev/null; then
        echo -e "${GREEN}✓ 已配置${NC}"
    else
        echo -e "${YELLOW}⚠ 未配置${NC}"
    fi
fi

echo ""
echo "6. 前端路由检查"
echo "================"

# 检查路由配置
if [ -f "./RefundAuditAndroid/src/router/index.js" ]; then
    echo -n "检查企业微信测试路由 ... "
    if grep -q "wechat-test" "./RefundAuditAndroid/src/router/index.js" 2>/dev/null; then
        echo -e "${GREEN}✓ 已配置${NC}"
    else
        echo -e "${RED}✗ 未配置${NC}"
    fi
fi

echo ""
echo "7. 部署建议"
echo "============"

echo "✓ 确保企业微信应用已在管理后台正确配置"
echo "✓ 确保可信域名已添加到企业微信应用配置"
echo "✓ 确保SSL证书已正确配置（生产环境必需）"
echo "✓ 确保防火墙已开放相应端口"
echo "✓ 建议配置反向代理（Nginx）"
echo "✓ 建议配置进程管理器（PM2/Supervisor）"

echo ""
echo "8. 测试建议"
echo "============"

echo "1. 在企业微信中访问: $FRONTEND_URL"
echo "2. 测试企业微信登录功能"
echo "3. 访问测试页面: $FRONTEND_URL/#/wechat-test"
echo "4. 检查浏览器控制台是否有错误"
echo "5. 检查网络请求是否正常"

echo ""
echo "=== 验证完成 ==="
echo "结束时间: $(date)"